package com.ljc.cjgl.controller.bbs;

import com.ljc.cjgl.entitiy.Course;
import com.ljc.cjgl.entitiy.bbs.Article;
import com.ljc.cjgl.entitiy.bbs.Comment;
import com.ljc.cjgl.form.bbs.ArticleForm;
import com.ljc.cjgl.form.bbs.CommentForm;
import com.ljc.cjgl.service.CourseService;
import com.ljc.cjgl.service.bbs.ArticleService;
import com.ljc.cjgl.service.bbs.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author: JavaJun
 * Date: 2019-04-28 16:19
 */
@Controller
@RequestMapping(value = "/bbs/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private CourseService courseService;

    @GetMapping(value = "/{id}")
    public String detailView(@PathVariable Integer id, ModelMap map) {
        Article article = articleService.findById(id);
        map.put("article", article);
        List<Comment> comments = commentService.findByArticleId(id);
        map.put("commentList", comments);
        return "bbs/article/detail";
    }

    /**
     * 新增帖子页面
     * @return
     */
    @GetMapping(value = "")
    public String createView(ModelMap map) {
        ArticleForm form = new ArticleForm();
        List<Course> courseList = courseService.findAll();
        map.put("courseList", courseList);
        return "bbs/article/create";
    }

    /**
     * 新增帖子
     * @return
     */
    @PostMapping(value = "")
    @ResponseBody
    public Map<Object, Object> create(ArticleForm form) {
        Article save = articleService.save(form);
        Map<Object, Object> success = new HashMap<>();
        success.put("success", "200");
        success.put("msg", "发布新帖成功");
        success.put("id", save.getId());
        return success;
    }

    @PostMapping(value = "/{articleId}/comment")
    @ResponseBody
    public Map<Object, Object> createComment(@PathVariable Integer articleId, CommentForm form) {
        Comment save = commentService.save(form);
        Map<Object, Object> success = new HashMap<>();
        success.put("success", "200");
        success.put("msg", "发布新帖成功");
        return success;
    }
}
